package com.idea.id_generate.exception;

import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;

public class ApiException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private final String message;

    private final int code;

    private final HttpStatus httpStatus;

    public ApiException(String message, int code) {
        this.message = message;
        this.code = code;
        this.httpStatus = HttpStatus.OK;
    }

    public ApiException(String message, int code, HttpStatus httpStatus) {
        this.message = message;
        this.code = code;
        this.httpStatus = httpStatus;
    }

    public ApiException(FieldError fieldError, int code) {
        this.message = fieldError.getField() + " " + fieldError.getDefaultMessage();
        this.code = code;
        this.httpStatus = HttpStatus.OK;
    }

    public String getMessage() {
        return message;
    }

    public int getCode() {
        return code;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ApiException{");
        sb.append("message='").append(message).append('\'');
        sb.append(", code=").append(code);
        sb.append(",httpStatus=").append(httpStatus);
        sb.append('}');
        return sb.toString();
    }
}
